// pages/income/income.js
// 获取应用实例
const app = getApp();
import http from '../../utils/http.js';
import util from '../../utils/util.js'

Page({

  /**
   * 页面的初始数据
   */
    data: {
      businessId:'',
      totalData:{},
      yesterdayInfo:{},
      list:[],
      pageFlag :false,
      startTime:'',
      endTime:'',
      tipFlag:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let that = this
      that.setData({
        businessId: options.businessId,
        startTime: this.getStartTime(util.getDefaultDate()),
        endTime: this.getEndTime(util.getDefaultDate()),
      })
      this.GetTotalMed()
      this.GetYesterdayMed()
      this.GetProfitMed()
    },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    wx.showLoading()
    this.setData({
      totalData: {},
      list: [],
      pageFlag: false,
      startTime: this.getStartTime(util.getDefaultDate()),
      endTime: this.getEndTime(util.getDefaultDate())
    }, () => {
      this.GetTotalMed()
      this.GetProfitMed()
      setTimeout(function(){
        // 取消标题栏进度条效果
        wx.hideNavigationBarLoading()
        // 取消页面刷新动画
        wx.stopPullDownRefresh()
        wx.hideLoading()
      }, 1000)
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    if (!that.data.pageFlag) {
      this.GetProfitMed()
    }
  },

  /**
   * 请求列表
   */
   GetTotalMed(){
    let that = this
    http.post('/api/store/v1/store/profit/census', {
      id: that.data.businessId
    }).then(res => {
      if (res.code === 200) {
        that.setData({
          totalData: res.data
        })
      } else {
        wx.showToast({
          title: res.message,
          icon:'none',
          duration:2000
        })
      }
    })
  },

  GetProfitMed(){
      let that = this
      http.post('/api/store/v1/store/profit', {
        id: that.data.businessId,
        startTime:that.data.startTime,
        endTime:that.data.endTime
      }).then(res => {
        if (res.code === 200) {
          if (res.data.length > 0) {
            let arr = that.data.list
            arr = arr.concat(res.data)
            let nextEnd = res.data[res.data.length-1].profitDate + '-01' // 下个nextEnd                        
            let endTime = nextEnd.split('-')[0] + '/' + nextEnd.split('-')[1] + '/' + '01'
            if (new Date(endTime).getTime()<=new Date('2022/04/01').getTime()) {
              that.setData({
                pageFlag: true
              })
            }
            for (var i = 0 ; i < arr.length ;i++) {
              let date = ''
              date = arr[i].profitDate.split('-')[0] + '年' + arr[i].profitDate.split('-')[1] + '月'
              arr[i].date = date
            }
            that.setData({
              list: arr,
              startTime: that.getStartTime(nextEnd),
              endTime: that.getEndTime(nextEnd)
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon:'none'
          })
        }
      })
  },

  GetYesterdayMed(){
    let that = this;
    let yesterday = new Date(new Date().getTime()-1000*60*60*24)
    let year = yesterday.getFullYear()
    let month = Number(yesterday.getMonth()+1)>=10?Number(yesterday.getMonth()+1):'0'+Number(yesterday.getMonth()+1)
    let day = Number(yesterday.getDate())>=10?Number(yesterday.getDate()):'0'+Number(yesterday.getDate())
    yesterday = year+'-'+month+'-'+day
    http.post('/api/store/v1/store/profit', {
      id: that.data.businessId,
      startTime: yesterday + ' 00:00:00',
      endTime: yesterday + ' 23:59:59',
    }).then(res => {
      if (res.code === 200) {
        if(res.data.length > 0){
          that.setData({
            yesterdayInfo: res.data[0]
          })
        }
      } else {
        wx.showToast({
          title: res.message,
          icon:'none',
          duration:2000
        })
      }
    })
  },

  // 结算说明
  showMask(){
    this.setData({
      tipFlag:true
    })
  },
  // 关闭弹窗
  closeMask(){
    this.setData({
      tipFlag: false
    })
  },
  getStartTime(date) {
    const myDate = new Date(date)
    let tYear = myDate.getFullYear()
    let tMonth = myDate.getMonth() -1 //上月
    tYear = Number(tMonth) <= 0 ?  Number(tYear)-1 : tYear
    tMonth = tMonth <= 0 ? Number(tMonth) + 12 : tMonth
    // 月份补0
    if (tMonth < 10) {
      tMonth = '0' + tMonth
    }
    return tYear + '-' + tMonth + '-01' + ' ' + '00:00:00'
  },

  getEndTime(date){
    const myDate = new Date(date)
    let tYear = myDate.getFullYear()
    let tMonth = myDate.getMonth() + 1 // 本月
    let nextMonth = myDate.getMonth() + 2 // 下下月
    let nextMonthDate = tYear + '/' + nextMonth + '/' + '01'
    const nextDate = new Date(nextMonthDate).getTime() - 1000 * 60 * 60 * 24 // 下下月-1天
    const tDay = new Date(nextDate).getDate()
    // 月份补0
    if (tMonth < 10) {
      tMonth = '0' + tMonth
    } 
    return tYear + '-' + tMonth + '-' + tDay + " " + "23:59:59"
  }
})